number = int(input()) 
vsego = 0 
a = number // 1000 
b = number % 1000 // 100 
c = number % 100 // 10 
d = number % 10 
numSecond = [0, 0, 0, 0] 
numThird = [0, 0, 0, 0, 0, 0] 
numFourth = [0, 0, 0, 0] 
num = [a, b, c, d, a*10+b, a*10+c, a*10+d, b*10+c, b*10+d, c*10+d, a*100+b*10+c, a*100+c*10+d, a*100+b*10+d, b*100+c*10+d, number] 
 
for q in range(1, number+1): 
    if number%q == 0: 
        vsego += 1 
if vsego == 2: 
    print(number) 
     
else: 
     
    for i in range(4): 
        vsego = 0 
        for w in range(1, max(num[13], num[12], num[11], num[10])+1): 
            if num[i+10]%w == 0: 
                vsego += 1 
        if vsego == 2: 
            numSecond[i] = num[i+10] 
    if numSecond[0] + numSecond[1] + numSecond[2] + numSecond[3] != 0: 
        print(max(numSecond[0], numSecond[1], numSecond[2], numSecond[3])) 
         
    else: 
         
        for r in range(6): 
            vsego = 0 
            for e in range(1, max(num[9], num[8], num[7], num[6], num[5], num[4])+1): 
                if num[r+4]%e == 0: 
                    vsego += 1 
            if vsego == 2: 
                numThird[r] = num[r+4] 
        if numThird[0] + numThird[1] + numThird[2] + numThird[3] + numThird[4] + numThird[5] != 0: 
            print(max(numThird[0], numThird[1], numThird[2], numThird[3], numThird[4], numThird[5]))
            
        else: 
            
            for t in range(4): 
                vsego = 0 
                for y in range(1, max(num[3], num[2], num[1], num[0])+1): 
                    if num[t]%y == 0: 
                        vsego += 1 
                if vsego == 2: 
                    numFourth[t] = num[t] 
            if numFourth[0] + numFourth[1] + numFourth[2] + numFourth[3] != 0: 
                print(max(numFourth[0], numFourth[1], numFourth[2], numFourth[3])) 
                
            else: 
                
                print('0')